using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

// dllimport
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Threading;

using myBase;

class Program
{
    static void Main()
    {
        Application.Run(new GrayForm());
    }
}

class GrayForm : BaseForm{
    private HiPerfTimer myTimer;
    private TextBox timeBox;

    public GrayForm(){
        Text = "灰度化";

        var pixelBtn = new Button();
        pixelBtn.Parent = this;
        pixelBtn.Name = "pixel";
        pixelBtn.Text = "像素操作";
        pixelBtn.Location = new Point(37, 200);
        pixelBtn.Click += new EventHandler(pixel_click);

        var memBtn = new Button();
        memBtn.Parent = this;
        memBtn.Name = "memory";
        memBtn.Text = "内存法";
        memBtn.Location = new Point(37, 246);
        memBtn.Click += new EventHandler(mem_click);

        var pointerBtn = new Button();
        pointerBtn.Parent = this;
        pointerBtn.Name = "close";
        pointerBtn.Text = "指针法";
        pointerBtn.Location = new Point(37, 292);
        pointerBtn.Click += new EventHandler(pointer_click);

        var label = new Label();
        label.Parent = this;
        label.Text = "运行时间";
        label.Location = new Point(37,356);

        timeBox = new TextBox();
        timeBox.Parent = this;
        timeBox.Name = "timeBox";
        timeBox.TextAlign = HorizontalAlignment.Right;
        timeBox.Location = new Point(37,375);

        myTimer = new HiPerfTimer();

    }

    private int getGray(Color color){
        return (int)(
                color.R * 0.299 
            +   color.G * 0.587
            +   color.B * 0.114
        );
    }

    private void pixel_click(object sender, EventArgs e){
        if(curBitmap != null){
            myTimer.Start();
            Color curColor;
            int ret;

            for(int i = 0;i < curBitmap.Width;i++){
                for(int j= 0; j < curBitmap.Height;j++){
                    curColor = curBitmap.GetPixel(i,j);
                    ret = getGray(curColor);
                    curBitmap.SetPixel(i,j,Color.FromArgb(ret,ret,ret));
                }
            }
            myTimer.Stop();
            timeBox.Text = myTimer.Duration.ToString("####.##")+"毫秒";
            Invalidate();
        }
    }
    private void mem_click(object sender, EventArgs e){
        if(curBitmap != null){
             myTimer.Start();

            var rect = new Rectangle(0,0,curBitmap.Width,curBitmap.Height);
            var bmpData = curBitmap.LockBits(
                rect,
                ImageLockMode.ReadWrite,
                curBitmap.PixelFormat  
            );
            IntPtr ptr = bmpData.Scan0;

            // 24位 每个像素3 bytes 需要复制的字节数
            int bytes = curBitmap.Width * curBitmap.Height * 3;
            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(
                ptr,rgbValues,
                0,bytes
            );

            double colorTemp = 0;
            for(int i =0; i < rgbValues.Length; i+=3){
                colorTemp = rgbValues[i+2]*0.299
                    + rgbValues[i+1]*0.587
                    + rgbValues[i]*0.114;

                rgbValues[i] = rgbValues[i+1] = rgbValues[i+2]
                    = (byte)colorTemp;
            }

             System.Runtime.InteropServices.Marshal.Copy(
                rgbValues,0,
                ptr,bytes
            );
            curBitmap.UnlockBits(bmpData);

            myTimer.Stop();
            timeBox.Text = myTimer.Duration.ToString()+"毫秒";

            Invalidate();
        }
    }
    private void pointer_click(object sender, EventArgs e){
        if(curBitmap != null){
            MessageBox.Show("暂不支持");
        }
    }
}
